/*----------------------------------------------------------------------------

	Copyright (c) 2006, Creator of the Cant Project.
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	+ Redistributions of source code must retain the above copyright notice,
	this list of conditions and the following disclaimer.

	+ Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.

	+ Neither the name of the Creator of the Cant Project nor the names of its
	contributors may be used to endorse or promote products derived from this
	software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------------*/

#include	"util.hpp"



/**
	Constant value.
	
*/
const std::string	Util::whitespace			= " \t\n";


/**
	Empty string.

*/
const std::string	Util::empty					= "";


/**
	Replaces all instances of one substring with another substring.
	
	@param		a_strOriginal
	@param		a_strFind
	@param		a_strReplace
	@return		string with replacements (if any) performed.
*/
std::string	Util::replace(const std::string& a_strOriginal, const std::string& a_strFind, const std::string& a_strReplace )
{
	//MW:	The string that will have replacements pereformed...
	std::string				strResult		= a_strOriginal;
	
	std::string::size_type	posFind			= 0;
	do
	{
		posFind		 		= strResult.find(a_strFind, posFind );
		if (posFind != std::string::npos )
		{
			strResult		= strResult.replace(posFind, a_strFind.size(), a_strReplace );
		}
	}
	while (posFind != std::string::npos );
	
	return	strResult;
}
		
		
/**
	Trims whitespace from beginning and end of a string.
	
	@param		a_strOriginal		string to be trimmed.
*/
std::string	Util::trim(const std::string& a_strOriginal )
{
	//MW:	Find first & last non-whitespace character...
	std::string::size_type	iFirstPos			= a_strOriginal.find_first_not_of(Util::whitespace );
	std::string::size_type	iLastPos			= a_strOriginal.find_last_not_of(Util::whitespace );
	
	return	a_strOriginal.substr((iFirstPos == std::string::npos ) ? 0 : iFirstPos,
									(iLastPos == std::string::npos ) ? iLastPos : iLastPos - (--iFirstPos ) );
}


/**
	Splits a string into two parts either side of a separator.
	
	@param		a_strOriginal		string to be split.
	@param		a_strSeparator		separator.
	@return		tuple containing two parts.
*/
StringPair	Util::split(const std::string& a_strOriginal, const std::string& a_strSeparator )
{
	 StringPair				pair;
	
	//MW:	Find the separator (if present)...
	std::string::size_type	iPos				= a_strOriginal.find(a_strSeparator, 0 );
	if (iPos != std::string::npos )
	{
		pair.left			= a_strOriginal.substr(0, iPos );
		pair.right			= a_strOriginal.substr(++iPos );
	}
	else
	{
		pair.left			= empty;
		pair.right			= a_strOriginal;
	}
	
	return	pair;
}

		
/**
	Converts a value to a string.
	
	@param		a_iValue		value to convert.
	@return		converted value.
*/
std::string	Util::toString(int a_iValue )
{
	ostringstream			streamConvert;
	
	streamConvert	<< a_iValue;
	
	return	streamConvert.str();
}
